<template>
  <div class="app-container-lr">
    <div class="app-left">
      <!-- 查询 -->
      <tl-list-left-search :query-param="queryParam" @search="doQuery">
        <el-form-item label="单据类型">
          <el-badge :value="totalCount" class="item" style="width:208px">
            <el-select v-model="queryParam.qry_sourcetype" clearable style="width:200px">
              <el-option v-for="item in sourcetypeList" :key="item.code" :label="item.name" :value="item.code">
                <span style="float: left">{{ item.name }}</span>
                <span class="count" v-if="item.count > 0">
                  <el-badge class="mark" :value="item.count" />
                </span>
              </el-option>
            </el-select>
          </el-badge>
        </el-form-item>
        <el-form-item label="单据编号">
          <el-input v-model="queryParam.qry_docno" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="申请部门">
          <tl-ref-tree-module v-model="queryParam.qry_deptcode" :initDisplayData.sync="displayDeptName" :queryTreeUrl="url.queryDeptTree" :queryDataUrl="url.queryDeptList" treeQueryKey="qry_parentid" :singleSelect="true"></tl-ref-tree-module>
        </el-form-item>
        <el-form-item label="申请人">
          <el-input v-model="queryParam.qry_username" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="工号">
          <el-input v-model="queryParam.qry_usercode" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="单据状态">
          <el-select v-model="queryParam.qry_state" clearable style="width:200px">
            <el-option v-for="item in stateList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="申请日期">
          <el-date-picker v-model="queryParam.qry_submitdate1" type="date" placeholder="开始时间" value-format="yyyy-MM-dd"> </el-date-picker>至
          <el-date-picker v-model="queryParam.qry_submitdate2" type="date" placeholder="结束时间" value-format="yyyy-MM-dd"> </el-date-picker>
        </el-form-item>
      </tl-list-left-search>
    </div>
    <div class="app-right">
      <div>
        <!-- 列表选择 -->
        <div class="table-alert-div" ref="selector">
          <el-alert type="success" :closable="false" class="tl-table-alert-info" effect="light">
            <template slot="title">
              已选择&nbsp;<a class="tl-table-alert-info-a1">{{ selectedRowKeys.length }}</a
              >项&nbsp;&nbsp;
              <a class="tl-table-alert-info-a2" @click="doClearSelected">全部清空</a>
            </template>
          </el-alert>
        </div>
        <!--表格渲染-->
        <el-table class="tl-table" ref="mainTable" :height="tableHeight" resizable-key="tableHeight" :resizable-refs="tableResizeRefs" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" row-key="id" border fit :row-class-name="doChangeRowClassName">
          <el-table-column prop="maintitle" label="主题" :show-overflow-tooltip="true" min-width="350px" sortable="custom" sort-by="flowins.maintitle"></el-table-column>
          <el-table-column prop="deptname" label="申请部门" :show-overflow-tooltip="true" width="200px" sortable="custom" sort-by="dept.fullname" align="center"></el-table-column>
          <el-table-column prop="username" label="申请人" width="130px" sortable="custom" sort-by="u.name" align="center"></el-table-column>
          <el-table-column prop="dispnodeid" label="审批节点名称" width="120px" sortable="custom" sort-by="nodeins.nodeid" align="center"></el-table-column>
          <el-table-column prop="planenddate" label="到期日期" width="100px" sortable="custom" sort-by="nodeins.planenddate" align="center">
            <template slot-scope="scope">
              {{ scope.row.planenddate | toDateTime('yyyy-MM-dd') }}
            </template>
          </el-table-column>
          <el-table-column prop="daynum" label="剩余天数" width="100px" sortable="custom" sort-by="daynum" align="center">
            <template slot-scope="scope">
              <el-tag :type="scope.row.daynum > 0 ? 'success' : 'danger'">{{ scope.row.daynum }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="submitdate" label="申请日期" width="100px" sortable="custom" sort-by="flowins.submitdate">
            <template slot-scope="scope">
              {{ scope.row.submitdate | toDateTime('yyyy-MM-dd') }}
            </template>
          </el-table-column>
          <el-table-column prop="startdate" label="开始日期" width="100px" sortable="custom" sort-by="nodeins.startdate">
            <template slot-scope="scope">
              {{ scope.row.startdate | toDateTime('yyyy-MM-dd') }}
            </template>
          </el-table-column>
          <el-table-column prop="dispsourcetype" label="单据类型" width="180px" sortable="custom" sort-by="flowins.sourcetype" align="center"></el-table-column>
          <el-table-column prop="extendfield1" label="单据编号" width="140px" sortable="custom" sort-by="flowins.extendfield1" align="center"></el-table-column>
          <el-table-column label="操作" width="100px" align="center" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-position" @click="openApprovalDialog(scope.row.id)" :disabled="scope.row.state === 2 || scope.row.state === 3 || scope.row.state === 8">审批</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 列表分页 -->
        <el-row class="pagebar" ref="pager">
          <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
        </el-row>
        <tl-approval ref="approvalForm" @refresh="doRefresh"></tl-approval>
      </div>
    </div>
  </div>
</template>

<script>
// import checkPermission from '@/utils/permission' // 权限判断函数
/** mixins */
import { mixin_list } from '@tapui/mixins/mixin_list'
import { queryDataCode } from '@tapui/api/dataCode'
import { getAction } from '@tapui/utils/http'
/** 子组件 */
import tlApproval from './approvalOne'
import tlSearch from '@tapui/components/Tap/List/ListSearch'
import tlToolbar from '@tapui/components/Tap/List/ListToolBar'
import tlRefTreeModule from '@tapui/components/Tap/Ref/RefTree'
export default {
  name: 'approvalList',
  components: { tlApproval, tlSearch, tlToolbar, tlRefTreeModule },
  mixins: [mixin_list],
  data() {
    return {
      description: '待审批列表页',
      url: {
        list: '/cbo/approvalNodeIns/queryApprovalList',
        count: '/cbo/approvalNodeIns/querySouceTypeCount',
        queryDeptTree: '/cbo/dept/queryTreeList',
        queryDeptList: '/cbo/dept/list'
      },
      stateList: [],
      sourcetypeList: [],
      sorterInfo: {
        // 排序参数
        sidx: 'nodeins.createon',
        sord: 'asc'
      },
      displayDeptName: '',
      isQueryOnCreated: false
    }
  },
  computed: {
    totalCount() {
      let total = 0
      this.sourcetypeList.forEach((m) => {
        total += m.count
      })
      return total
    }
  },
  methods: {
    openApprovalDialog(id) {
      this.$refs.approvalForm.nodeInsId = id
      this.$refs.approvalForm.visible = true
    },
    doRowDbClick(row, column, event) {
      if (row.state === 2 || row.state === 3 || row.state === 8) {
        return
      }
      this.openApprovalDialog(row.id)
    }, /** 刷新,页号不变*/
    doRefresh() {
       getAction(this.url.count).then((res) => {
      this.sourcetypeList = res.result
    })
      this.loadData({})
    }
  },
  created() {
    queryDataCode({ itemCodes: 'TapApprovalNodeState' }).then((res) => {
      this.stateList = res.result.TapApprovalNodeState
    })
    getAction(this.url.count).then((res) => {
      this.sourcetypeList = res.result
    })
    if (this.$route.query.sourcetype) {
      this.$set(this.queryParam, 'qry_sourcetype', this.$route.query.sourcetype)
    }
    this.loadData({})
  }
}
</script>
<style scoped lang="scss">
.count {
  float: right;
  .mark {
    padding-top: 5px;
  }
}
.red-bg {
  background: red;
}
</style>
